home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-ca.2 / irsim-ca / irsim-cap-9.2 / src / utils / Makedep / gen.c next >
C/C++ Source or Header  |  1990-12-23  |  4KB  |  152 lines

  1. /*
  2.  * Module which generates the list of source files and which generates
  3.  * the include file dependencies.
  4.  */
  5.  
  6.  
  7. #include "makedep.h"
  8.  
  9. /* External and forward fcn declarations */
  10. extern StringList *GetIncludeFileList();
  11. StringList *FindIListLocation();
  12.  
  13.  
  14.  
  15. /*
  16.  * GenIncludeFileDependencies:
  17.  * Generate all include file dependencies.
  18.  * Starting with an empty list of include files, each source file 
  19.  * on SrcFiles is examined
  20.  * to extract all include file references it contains.  These file names
  21.  * are placed on an include file list, IList.  Also, each source file gets
  22.  * a linked list of (immediate) dependencies associated with it.
  23.  * Each include file on IList
  24.  * is examined to extract its include file dependencies; which are added to
  25.  * the list.  Also, each include file gets a linked list of (immediate)
  26.  * dependencies associated with it.
  27.  * The IList is kept in sorted order in order to remove duplicates
  28.  * and processed list entries are marked as such to prevent reprocessing.
  29.  */
  30.  
  31. GenIncludeFileDependencies()
  32.   {
  33.     StringList *p;
  34.     int done;
  35.  
  36.     if (Debug)
  37.       {
  38.     printf("\nGenIncludeFileDependencies:\n");
  39.       }
  40.  
  41.     IList = MakeList();        /* Create an empty IList. */
  42.     /* Run through the source files, generating each one's immediate
  43.        dependency list. */
  44.     for (p = SrcFiles->next; p != NULL; p = p->next)
  45.       {
  46.     GenDependencies(p);
  47.       }
  48.     /* Repeatedly iterate over IList until all elements have been processed. */
  49.     done = FALSE;
  50.     while (!done)
  51.       {
  52.         done = TRUE;        /* Assume we're done until proven otherwise. */
  53.     for (p = IList->next; p != NULL; p = p->next)
  54.       {
  55.         if (p->state == UNPROCESSED)
  56.           {
  57.             done = FALSE;
  58.         GenDependencies(p);
  59.           }
  60.       }
  61.       }
  62.   }
  63.  
  64.  
  65. /*
  66.  * GenDependencies:
  67.  * Generate the immediate dependency list for p and mark it as processed.
  68.  * Adds newly encountered include files to IList.  IList is kept in sorted
  69.  * order so that duplicates can be removed.
  70.  */
  71.  
  72. GenDependencies(p)
  73.     StringList *p;
  74.   {
  75.     StringList *il, *pil, *pIList;
  76.     DepList *dil;
  77.  
  78.     p->state = PROCESSED;
  79.     /* Generate a list of include files referenced directly in p. */
  80.     il = GetIncludeFileList(p);
  81.     /* Create a list of corresponding dependency records for p. */
  82.     for (pil = il->next; pil != NULL; pil = pil->next)
  83.       {
  84.     AddDepList(pil, p);
  85.       }
  86.  
  87.     if (Debug)
  88.       {
  89.         printf("%s ", p->str);
  90.     PrintDepList(p, "dependency list");
  91.       }
  92.  
  93.     /* Merge the list of include files into IList, discarding duplicates.
  94.        Make the pointers in p's dependency list point at the
  95.        records in IList rather than the il list.  This is simpler to program
  96.        than attempting to extract the non-duplicated records from il and
  97.        selectively updating things. */
  98.     for (dil = p->dep; dil != NULL; dil = dil->next)
  99.       {
  100.     pIList = FindIListLocation(dil->inclFile);
  101.                 /* pIList now points either to a record in
  102.                    IList for this include file or to the
  103.                    record in IList after which the include
  104.                    file should be inserted. */
  105.     if ((pIList == IList) || (!Equal(pIList->str, dil->inclFile->str)))
  106.       {
  107.         /* No duplicate - insert a new record into IList. */
  108.         InsertList(dil->inclFile->str, pIList);
  109.         pIList = pIList->next;
  110.                 /* Make pIList point at the new record. */
  111.       }
  112.     dil->inclFile = pIList;    /* pIList at this point points at the correct
  113.                    record in IList. */
  114.       }
  115.     /* Get rid of the il list of include files.  All references are now to
  116.        the records in IList. */
  117.     DestroyList(il);
  118.   }
  119.  
  120.  
  121. /*
  122.  * FindIListLocation:
  123.  * Search the (sorted) IList for a record corresponding to p.  If one is
  124.  * found then return a pointer to it.  Otherwise return a pointer to the
  125.  * record in IList after which such a record would appear.
  126.  */
  127.  
  128. StringList *FindIListLocation(p)
  129.     StringList *p;
  130.   {
  131.     StringList *ptr;
  132.     int order;
  133.  
  134.     for (ptr = IList; ptr->next != NULL; ptr = ptr->next)
  135.       {
  136.         order = strcmp(p->str, ptr->next->str);
  137.     if (order == 0)
  138.       {
  139.         /* Found a corresponding record.  Return a ptr to it. */
  140.         return(ptr->next);
  141.       }
  142.     else if (order < 0)
  143.       {
  144.         /* ptr->next->str is lexically greater than p->str.  p should
  145.            go before it but after ptr. */
  146.         return(ptr);
  147.       }
  148.       }
  149.     /* p goes at the end of the list. */
  150.     return(ptr);
  151.   }
  152.